$(function(){
    var URL = "http://localhost:9090/RegionStats"
    $.ajax({
        type:"GET",
        url:URL,
        data:{},
        dataType:"json",
        success:function (result) {
            var dataObj = result,
                count = 0,
                diff = 0,
                div_count = "",
                result = "";
            $.each(dataObj,function (index,item) {
                result+="<ul><li>"+item.state+"</li>";
                result+="<li>"+item.country+"</li>";
                result+="<li>"+item.latestTotalCases+"</li>";
                result+="<li>"+item.diffFromPrevDay+"</li></ul>";
                count+=item.latestTotalCases;
                diff+=item.diffFromPrevDay;
            });
            div_count+="<ul style='font-size: 30px'><li><strong>"+"全球累计确诊："+count+"</strong></li>";
            div_count+="<li><strong>"+"全球今日新增："+diff+"</strong></li>";
            console.log(result);
            console.log(div_count);
            $("#result").html(result);
            $("#div_count").html(div_count);
        }
    })
    $('.btn').on('click',function () {
        $('#result').html("")
        $.ajax({
            type:"GET",
            url:URL,
            data:{},
            dataType:"json",
            success:function (result) {
                var dataObj = result,
                    result = "",
                    count=0,
                    diff=0,
                    body_count = "";
                var search = $(".search").val();
                if(search==""){
                    search="all";
                }
                $.each(dataObj,function (index,item) {
                    if(search=="all"){
                        result+="<ul><li>"+item.state+"</li>";
                        result+="<li>"+item.country+"</li>";
                        result+="<li>"+item.latestTotalCases+"</li>";
                        result+="<li>"+item.diffFromPrevDay+"</li></ul>";
                    }
                    else if(search.toLowerCase()==item.state.toLowerCase()||search.toLowerCase()==item.country.toLowerCase()){
                        result+="<ul><li>"+item.state+"</li>";
                        result+="<li>"+item.country+"</li>";
                        result+="<li>"+item.latestTotalCases+"</li>";
                        result+="<li>"+item.diffFromPrevDay+"</li></ul>";
                        count+=item.latestTotalCases;
                        diff+=item.diffFromPrevDay;
                    }
                });
                if(search=="all"){

                }
                else{
                    body_count+="<ul style='width: 100%;display: inline;margin-top: 30px;font-size: 20px;'><li style='width: 48%;'><strong>"+"地区累计确诊："+count+"</strong></li>";
                    body_count+="<li style='width: 48%;'><strong>"+"地区新增确诊："+diff+"</strong></li>";
                }
                console.log(body_count);
                console.log(result);
                $("#body_count").html(body_count);
                $("#result").html(result);
            }
        })
    })
})